<cfcomponent displayname="Controller" extends="ModelGlue.unity.controller.Controller" output="false">

	<cffunction name="setSvnService" access= "public" returntype="void" output="false">	
		<cfargument name="svnService" type= "any" required="true">	
		<cfset variables.svnService = arguments.svnService/>
	</cffunction>

	<cffunction name="getSvnService" access= "public" returntype="any" output="false">	
		<cfreturn variables.svnService />
	</cffunction>

	<cffunction name="addMapping">
		<cfargument name="ModelGlue" required="true" type="any" />
		<cfargument name="thisViewMapping">
		<cfset var viewMappings = arguments.ModelGlue.getConfigSetting("viewMappings") />
		<cfif isArray(viewMappings)>
		  <cfset viewMappings = arrayToList(viewMappings) />
			<cfif NOT listFind(viewMappings,thisViewMapping)>
				<cfset modelglue.getViewRenderer().addViewMapping(thisViewMapping) />
			</cfif>	
		<cfelse>
			<cfif NOT listFind(viewMappings,thisViewMapping)>
				<cfset arguments.ModelGlue.setConfigSetting("viewMappings",listAppend(arguments.ModelGlue.getConfigSetting("viewMappings"),thisViewMapping))>
			</cfif>	
		</cfif>
	</cffunction>


	<cffunction name="init" access="public" returntype="any" hint="cfsvn object constructor.">
		<cfargument name="ModelGlue" required="true" type="ModelGlue.ModelGlue" />
		<cfargument name="InstanceName" required="true" type="string" />
		<cfset super.init( arguments.ModelGlue ) />
		<cfset addMapping(ModelGlue,"/actionpacks/cfsvn/views") />
		<cfset addMapping(ModelGlue,"/actionpacks/cfsvn/views/resource") />
		<cfreturn this />
	</cffunction>


	<cffunction name="getRepoTreeQry" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var resourceUrl = arguments.event.getValue("resourceUrl")>
	  <cfset var repoTreeQry = getSvnService().List("/")>	  
	  <cfset arguments.event.setValue("repoTreeQry",repoTreeQry)>
	</cffunction>
	
	<cffunction name="getRepoResource" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var resourceUrl = arguments.event.getValue("resourceUrl",arguments.event.getArgument("resourceUrl"))>
	  <cfset var revision = val(listLast(resourceUrl,":"))>
	  <cfif revision eq 0><cfset revision = -1></cfif>
	  <cfset repoResource = getSvnService().fileVersion(listFirst(resourceUrl,":"),revision)>
	  <cfset arguments.event.setValue("repoResource",repoResource)>
	</cffunction>

	<cffunction name="getRepoResourceHistory" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var resourcePath = listFirst(arguments.event.getValue("resourceUrl"),":")>
	  <cfset var historyQry = getSvnService().History(resourcePath)>
	  <cfset arguments.event.setValue("resourceHistory",historyQry)>
	</cffunction>

	<cffunction name="getRepoChangeSet" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var firstRevision = arguments.event.getValue("firstRevision")>
	  <cfset var lastRevision = arguments.event.getValue("lastRevision")>
	  <cfset arguments.event.setValue("changeSet",getSvnService().changeSet(firstRevision,lastRevision))>
	</cffunction>

	<cffunction name="addResource" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var resourceUrlField = arguments.event.getArgument("resourceUrlField","resourceUrl")>
	  <cfset var commitMessageField = arguments.event.getArgument("commitMessageField","commitMessage")>
	  <cfset var fileDataField = arguments.event.getArgument("fileDataField","fileData")>
	  <cfset var url = arguments.event.getValue(resourceUrlField)>
	  <cfset var commitMessage = arguments.event.getValue(commitMessageField)>
	  <cfset var fileData = arguments.event.getValue(fileDataField)>
	  <cfdump var="#getSvnService().addResource(url,commitMessage,fileData)#"><cfabort >
	  <cfset arguments.event.setValue("url",getSvnService().addResource(url,commitMessage,fileData))>
	</cffunction>

	<cffunction name="addOrUpdateResource" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var resourceUrlField = arguments.event.getArgument("resourceUrlField","resourceUrl")>
	  <cfset var commitMessageField = arguments.event.getArgument("commitMessageField","commitMessage")>
	  <cfset var fileDataField = arguments.event.getArgument("fileDataField","fileData")>
	  <cfset var url = arguments.event.getValue(resourceUrlField)>
	  <cfset var commitMessage = arguments.event.getValue(commitMessageField)>
	  <cfset var fileData = arguments.event.getValue(fileDataField)>
<!---
 	  <cfdump var="#getSvnService().addOrUpdateResource(url,commitMessage,fileData)#"><cfabort >
 --->
	  <cfset arguments.event.setValue("commitResults",getSvnService().addOrUpdateResource(url,commitMessage,fileData))>
	</cffunction>

	<cffunction name="modifyResource" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var url = arguments.event.getValue("resourceUrl")>
	  <cfset var commitMessage = arguments.event.getValue("commitMessage")>
	  <cfset var fileData = arguments.event.getValue("fileData")>
	  <cfset arguments.event.setValue("url",getSvnService().modifyResource(url,commitMessage,fileData))>
	</cffunction>

	<cffunction name="uploadResource" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var url = arguments.event.getValue("resourceUrl")>
	  <cfset var commitMessage = arguments.event.getValue("commitMessage")>
	  <cfset var fileData = arguments.event.getValue("fileData")>
	  <cfset arguments.event.setValue("url",getSvnService().addResource(url,fileData))>
	</cffunction>

	<cffunction name="createRepository" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var reposFilePath = arguments.event.getValue("url","C:/tmp/superRepo")>
	  <cfset var overwrite = arguments.event.getValue("fileData","false")>
	  <cfset arguments.event.setValue("url",getSvnService().createRepository(reposFilePath,overwrite))>
	</cffunction>

	<cffunction name="zipExport" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var firstRevision = arguments.event.getValue("firstRevision")>
	  <cfset var lastRevision = arguments.event.getValue("lastRevision")>
	  <cfset arguments.event.setValue("zipFile",getSvnService().zipExport(firstRevision,lastRevision))>
	</cffunction>

	<cffunction name="export" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var resourceUrl = arguments.event.getValue("resourceUrl")>
	  <cfset arguments.event.setValue("zipFile",getSvnService().export(resourceUrl))>
	</cffunction>


</cfcomponent>